﻿//=======================================================
// Author: Jonas Follesø <jonas@follesoe.no>
// Copyright (c) 2008
//
// Licensed under the Microsoft Public License (Ms-PL)
// A copy of the license is available at 
// http://www.opensource.org/licenses/ms-pl.html
//=======================================================

using System;
using System.Xml.Linq;
using Microsoft.Win32;

namespace Colorful.Control.Services
{
    internal class ExpressionDesignSwatchSaver : ISaveColors
    {
        public bool Save(Palette palette)
        {
            var saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = "xml";
            saveDialog.Filter = "Expression Design Swatch Library (.xml)|*.xml";
            saveDialog.FileName = palette.Name + ".xml";

            bool? result = saveDialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                var ns = XNamespace.Get("http://schemas.microsoft.com/expression/design/2007");
                var xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
                var xsd = XNamespace.Get("http://www.w3.org/2001/XMLSchema");

                var swatchXml = new XElement(ns + "SwatchLibrary",
                                    new XAttribute("xmlns", ns.NamespaceName),
                                    new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
                                    new XAttribute(XNamespace.Xmlns + "xsd", xsd.NamespaceName),
                                    new XAttribute("Name", palette.Name),
                                    new XElement(ns + "SolidColorSwatch", new XAttribute("Color", palette.Color1.ToString())),
                                    new XElement(ns + "SolidColorSwatch", new XAttribute("Color", palette.Color2.ToString())),
                                    new XElement(ns + "SolidColorSwatch", new XAttribute("Color", palette.Color3.ToString())),
                                    new XElement(ns + "SolidColorSwatch", new XAttribute("Color", palette.Color4.ToString())),
                                    new XElement(ns + "SolidColorSwatch", new XAttribute("Color", palette.Color5.ToString()))
                                    );

                swatchXml.Save(saveDialog.FileName);
                return true;                                
            }
            return false;
        }
    }
}
